//
//  DDOrderController.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/28.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "DDOrderController.h"
#import "ServiceImformationCell.h"
#import "ContactImformationCell.h"
#import "CarImformationCell.h"
#import "PayImformationCell.h"
#import "PayView.h"
#import "PaySectionView.h"
#import "SelectCarController.h"
#import "CouponsController.h"

@interface DDOrderController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) PayView *payView;

@end

@implementation DDOrderController


#pragma mark - ********************生命周期********************

- (void)viewDidLoad {
    [super viewDidLoad];
    //计划周期数
    int n = 52;
    //单位周期数
    int m = 7;
    //投资金额
    CGFloat money = 10000;
    //基础利率
    CGFloat a = 0.0788;
    //最高利率
    CGFloat b = 0.1188;
    //递增利率
    CGFloat c = 0.005;
    //加息利息
    CGFloat temp = 0;
    //总利息
    CGFloat total = 0;
    //计算利率
    CGFloat r ;
    
    for (int index = 1; index <= n; index++) {
        if ((a + c*(index-1) >= b)) {
            r = b;
        } else {
            r = (a + c*(index-1));
        }
        total = total + (r * (money * m) + temp)/365;
        NSLog(@"r = %.4f      r * (money * m)/365 = %.4f      total = %.4f",r,r * (money * m)/365,total);
    }
}



#pragma mark - ********************配置信息********************

- (void)configuration {
    [self showCustomNavView];
    self.navTitle = @"订单信息";
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.payView];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(64, 0, kUI_CellHeight * 1.5, 0));
    }];
    [self.payView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(self.tableView.mas_bottom);
    }];
}

- (void)bindViewModel {
    self.payView.viewModel = self.viewModel;
}

#pragma mark - ********************基础方法********************


#pragma mark - ********************代理方法********************

#pragma mark - UITableViewDataSource
//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    @weakify(self);
    if (indexPath.section == 0) {
        ServiceImformationCell *cell = [ServiceImformationCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    } else if (indexPath.section == 1) {
        ContactImformationCell *cell = [ContactImformationCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    } else if (indexPath.section == 2) {
        CarImformationCell *cell = [CarImformationCell cellWithTableView:tableView indexPath:indexPath];
        cell.carNumberView.inputTextfiled.surpView = self.tableView;
        cell.carNumberView.inputTextfiled.currentHeight = (kUI_CellHeight * 8 + 30 * 3 + 64);
        cell.viewModel = self.viewModel;
        cell.selectCarTypeCompleted = ^{
            @strongify(self);
            [self gotoSelectCarTypeCtrl];
        };
        return cell;
    }
    PayImformationCell *cell = [PayImformationCell cellWithTableView:tableView indexPath:indexPath];
    cell.viewModel = self.viewModel;
    cell.selectCouponsCompleted = ^{
        @strongify(self);
        [self gotoSelectCouponseCtrl];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        return kUI_CellHeight * 3 + 10;
    }
    return kUI_CellHeight * 2 + 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    PaySectionView *sectionView = [PaySectionView new];
    sectionView.titleLabel.text = self.viewModel.sectionTitles[section];
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

#pragma mark - ********************响应事件********************
/**
 跳转到车辆型号选择VC
 */
- (void)gotoSelectCarTypeCtrl {
    @weakify(self);
    SelectCarController *ctrl = [SelectCarController new];
    ctrl.selectCompleted = ^(NSString *carBrand) {
        @strongify(self);
        self.viewModel.carBrand = carBrand;
    };
    [self.navigationController pushViewController:ctrl animated:YES];
}

/**
 跳转到优惠券选择VC
 */
- (void)gotoSelectCouponseCtrl {
    @weakify(self);
    CouponsController *ctrl = [CouponsController new];
    ctrl.selectCouponseCompleted = ^(NSString *coupon) {
        @strongify(self);
        self.viewModel.order_coupon = coupon;
        self.viewModel.order_payPrice = self.viewModel.order_packagePrice - [coupon floatValue];
    };
    [self.navigationController pushViewController:ctrl animated:YES];
}

#pragma mark - ********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.rowHeight = kUI_CellHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (PayView *)payView {
    if (!_payView) {
        _payView = [PayView new];
    }
    return _payView;
}

- (OrderViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [OrderViewModel new];
    }
    return _viewModel;
}
#pragma mark - ********************网络模型********************

@end
